package com.heima.user.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thread.AppThreadLocalUtil;

@Service
public class ApUserDataServiceImpl implements ApUserDataService {
    @Autowired
    private CacheService cacheService;

    //关注
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登录用户
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //保存数据
        if (dto.getOperation() == 0) {
            //关注 ,保存到redis
            cacheService.hPut("USERFOLLOW_" + apUser.getId(), dto.getAuthorId().toString(), JSON.toJSONString(dto));
        } else {
            //取消,删除redis
            cacheService.hDelete("USERFOLLOW_" + apUser.getId(), dto.getAuthorId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
